<template>
  <div class="seven-days-event-count">
    <split-line :text="title" icon @iconclick="showEventList" />
    <div class="seven-days-event-count-legend" v-if="legend.length">
      <div
        v-for="(item, index) of legend"
        :key="index"
        :style="{ color: item.color }"
        class="seven-days-event-count-legend-item"
      >
        <span style="color: #ffffff">{{ item.label }}</span>
      </div>
    </div>
    <echarts-gradient-mutil-line
      v-if="data.days.length"
      :axis-data="data.days"
      :series-data="[data.data1, data.data2]"
      :tooltip-formatter="formatter"
      style="height: 200px"
    />
    <empty v-else />
  </div>
</template>

<script>
import SplitLine from '@/components/split-line.vue';
import EchartsGradientMutilLine from '@/components/charts/echarts-gradient-mutil-line';
import Empty from '@gis/lib/components/Empty/index.vue';
import timeFormat from '@gis/lib/utils/time-format.js';
import { useUserInfoStore } from '@gis/lib/pinia/userInfo.js';

export default {
  name: 'seven-days-event-count',
  components: { Empty, EchartsGradientMutilLine, SplitLine },
  data() {
    return {
      title: '七天事件统计',
      legend: [],
      data: {
        days: [],
        data1: {
          data: [],
          color: [],
        },
        data2: {
          data: [],
          color: [],
        },
      },
    };
  },
  computed: {
    areaCode() {
      const { area_code } = useUserInfoStore();
      return area_code;
    },
  },
  watch: {
    areaCode: {
      immediate: true,
      handler() {
        this.getDataAsync();
      },
    },
  },
  created() {
    this.getDataAsync();
  },
  methods: {
    formatter(data) {
      const getText = (data) => {
        return data.seriesName + '&nbsp;&nbsp;&nbsp;' + data.value;
      };
      return getText(data[0]) + '<br/>' + getText(data[1]);
    },
    async getDataAsync() {
      const params = { area_code: this.areaCode };
      if (!params.area_code) {
        return;
      }
      const res = await this.$axios.get('/xw-microservice/xw-analysis/v3/event/count/week', { params });
      const data = res.data || {};
      const days = data.days || [];
      const date = days.map((item) => {
        return (item = timeFormat.getMonthDate(item));
      });
      const value = [data.xzs, data.bjs];
      const type = ['上报数量', '办结数量'];
      const colors = ['rgba(13, 194, 181,0.6)', 'rgba(224, 129, 69,0.6)'];
      this.legend = type.map((item, index) => {
        return { label: item, color: colors[index] };
      });
      this.data = {
        days: date,
        data1: {
          name: type[0],
          data: value[0] || [],
          color: [
            { offset: 0, color: 'rgba(113, 194, 181,0.6)' },
            { offset: 1, color: 'rgba(113, 194, 181,0)' },
          ],
        },
        data2: {
          name: type[1],
          data: value[1] || [],
          color: [
            { offset: 0, color: 'rgba(224, 129, 69,0.6)' },
            { offset: 1, color: 'rgba(224, 129, 69,0)' },
          ],
        },
      };
    },
    showEventList() {
      /*dialog({
        content: () => import('@/views/wise-governance/components/event-list-dialog.vue'),
        dialogProps: {
          title: '事件列表',
          width: '1300px',
          bodyStyle: { height: '730px' },
        },
      });*/
    },
  },
};
</script>

<style scoped lang="less">
.seven-days-event-count {
  position: relative;
  .seven-days-event-count-legend {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    padding-right: 20px;
    .seven-days-event-count-legend-item {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      &:before {
        content: '';
        display: block;
        width: 14px;
        height: 7px;
        margin-right: 5px;
        background: currentColor;
      }
      &:not(:last-child) {
        margin-right: 25px;
      }
    }
  }
}
</style>
